<template>
  <el-dialog :title="title" :visible.sync="dialogVisible" :close-on-click-modal="false" @close="close" width="500px" append-to-body>
    <div class="form-box">
      <el-form ref="form" :model="form" :inline="true" :rules="rules" label-width="100px" class="demo-ruleForm">
        <el-form-item label="腾讯" prop="1" v-if="type==6">
          <el-input v-model="form[1]" placeholder="请输入腾讯冷却期">
            <span slot="suffix">天</span>
          </el-input>
        </el-form-item>
        <el-form-item label="QQ" prop="qq" v-if="type==7">
          <el-input v-model="form['qq']" placeholder="请输入QQ冷却期">
            <span slot="suffix">天</span>
          </el-input>
        </el-form-item>
        <el-form-item label="微信" prop="wx" v-if="type==7">
          <el-input v-model="form['wx']" placeholder="请输入微信冷却期">
            <span slot="suffix">天</span>
          </el-input>
        </el-form-item>
        <el-form-item label="网易" prop="2">
          <el-input v-model="form[2]" placeholder="请输入网易冷却期">
            <span slot="suffix">天</span>
          </el-input>
        </el-form-item>
        <el-form-item label="米哈游" prop="3">
          <el-input v-model="form[3]" placeholder="请输入米哈游冷却期">
            <span slot="suffix">天</span>
          </el-input>
        </el-form-item>
        <el-form-item label="Steam" prop="5">
          <el-input v-model="form[5]" placeholder="请输入Steam冷却期">
            <span slot="suffix">天</span>
          </el-input>
        </el-form-item>
        <el-form-item label="其他" prop="4">
          <el-input v-model="form[4]" placeholder="请输入其他游戏冷却期">
            <span slot="suffix">天</span>
          </el-input>
        </el-form-item>

        <div class="center">
          <el-button @click="close()">取消</el-button>
          <el-button type="primary" @click="submitForm('form')" :loading="loading">提交</el-button>
        </div>
      </el-form>
    </div>
  </el-dialog>
</template>

<script>
import phoneApi from "@/api/phoneManage";
import {
  addtenantPushSettings,
  getTenantPushSettingsByTenantId,
  delTenantPushSettings,
} from "@/api/system/shopManage";
export default {
  name: "",
  data() {
    return {
      dialogVisible: false,
      loading: false,
      isEdit: false,
      title:'冷却设置',
      // 表单参数
      form: {
        1: "",
        2: "",
        3: "",
        4: "",
        5: "",
        qq: "",
        wx: "",
      },
      settingId: "",
      type: 6,
      rules: {},
    };
  },
  created() {},
  computed: {
    userInfo() {
      return this.$store.getters.userInfo;
    },
    shopInfo() {
      return this.$store.getters.shopInfo;
    },
  },
  methods: {
    // 打开弹窗
    open(flag) {
      if (flag == "phone") {
        this.type = 6;
        this.title = '手机号冷却设置'
      }
      if (flag == "goods") {
        this.type = 7;
        this.title = '商品冷却设置'
      }
      this.dialogVisible = true;
      this.getList();
    },
    // 重置
    reset() {
      this.$refs.form.resetFields();
    },
    // 获取
    getList() {
      getTenantPushSettingsByTenantId({
        tenantId: this.shopInfo.tenantId,
        type: this.type, //6 手机号冷却设置  7 商品冷却设置
      }).then((res) => {
        if (res.data && res.data.settingsJson) {
          this.settingId = res.data.id;
          this.form = JSON.parse(res.data.settingsJson);
        }
      });
    },

    // 保存到后台
    save() {
      if (this.dialogVisible) {
        this.loading = true;
        addtenantPushSettings({
          tenantId: this.shopInfo.tenantId,
          tenantName: this.shopInfo.tenantName,
          settingsJson: JSON.stringify(this.form),
          type: this.type,
        })
          .then((res) => {
            this.loading = false;
            this.dialogVisible = false;
            this.$message.success(res.msg);
          })
          .catch((err) => {
            this.loading = false;
          });
      }
    },

    submitForm(formName) {
      this.$refs["" + formName].validate((valid) => {
        if (valid) {
          this.save();
        } else {
          this.$alert("请填写完整！");
        }
      });
    },

    close() {
      this.reset();
      this.dialogVisible = false;
    },
  },
};
</script>
<style scoped lang="css">
</style>
